﻿// Copyright (c) 2009 Paul Kohler
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
// and associated documentation files (the "Software"), to deal in the Software without restriction, 
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do 
// so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial 
// portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.IO;
using FileSystemDatabase.Exceptions;
using FileSystemDatabase.Tests.Models;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using System.Collections.Generic;

namespace FileSystemDatabase.Tests
{
	[TestFixture]
	public class JsonDatabase_load_tests
	{
		#region Setup/Teardown

		[SetUp]
		public void TestSetUp()
		{
			_db = new JsonDatabase(_path);
		}

		#endregion

		[TestFixtureSetUp]
		public void TestFixtureSetUp()
		{
			_path = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\SampleData"));
		}

		private string _path;
		private JsonDatabase _db;

		[Test]
		public void Read_in_a_single_Person_object()
		{
			Person person101 = _db.Read<Person>("101");
			
			Assert.That(person101, Is.Not.Null);
			Assert.That(person101.PersonId, Is.EqualTo("101"));
			Assert.That(person101.Name, Is.EqualTo("Jack"));
			Assert.That(person101.DOB, Is.EqualTo(new DateTime(1968, 3, 7)));

			//Assert.That(person101.Extra, Is.Null);
		}

		[Test]
		public void Read_all_rows_of_Person()
		{
			var rows = _db.Read<Person>();
			List<Person> people = new List<Person>(rows);

			Assert.That(people.Count, Is.EqualTo(2));
			Assert.That(people[0].PersonId, Is.EqualTo("101"));
			Assert.That(people[0].Name, Is.EqualTo("Jack"));
			Assert.That(people[1].PersonId, Is.EqualTo("102"));
			Assert.That(people[1].Name, Is.EqualTo("Jill"));
		}

		[Test]
		public void Read_in_a_Pet()
		{
			Pet pet = _db.Read<Pet>("bad70af3-7145-44c0-bb4d-0aae048a5628");

			Assert.That(pet, Is.Not.Null);
			Assert.That(pet.ID, Is.EqualTo("bad70af3-7145-44c0-bb4d-0aae048a5628"));
			Assert.That(pet.Name, Is.EqualTo("Billy"));
		}

		[Test]
		public void There_is_only_1_dog()
		{
			var rows = _db.Read<Pet>();
			Assert.That(rows.Count, Is.EqualTo(1));
		}

		[Test]
		[ExpectedException(typeof(FileSystemDatabaseReadException), ExpectedMessage = @"The character '\' is not a valid object identifier.")]
		public void Trying_to_read_an_item_with_a_backslash_throws_an_error()
		{
			_db.Read<Pet>("\\etc");
		}

		[Test]
		[ExpectedException(typeof(FileSystemDatabaseReadException), ExpectedMessage = @"The character '>' is not a valid object identifier.")]
		public void Trying_to_read_an_item_with_a_bracket_throws_an_error()
		{
			_db.Read<Pet>("123>etc");
		}
	}
}